import { getTi } from "../../utils/api.js";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        duration: 0,
        current: 0,
        index: 0,
        smallNum: 0,
        path: 'https://img.yuncoding.cn/souti/book',
        showModalStatus: false,
        arr: ['全部'],
        bgStatus: false
    },

    //获取大题个数
    getTiNum: function () {
        var tiNumber = this.data.tiNumber;
        var grids = [];
        for (let i = 1; i <= tiNumber; i++) {
            grids.push(i);
        }
        this.setData({
            grids: grids,
            tiNumber: tiNumber
        })
        // this.getSmallTi(1); //获取第一大题的小题个数
    },
    feedBack: function () {
        this.setData({
            ModalStatus: true,
            btnStatus: true,
            textareaStatus: true,
            flag: true,
            height: '720rpx'
        })
    },
    unScale: function () {
        this.setData({
            showModalStatus: false,
        })
    },
    //放大图片
    zoomImg: function () {
        var winWidth = this.data.winWidth;
        var current = this.data.current;
        var imgWidth = this.data.ti[current].imgWidth;
        var imgHeight = this.data.ti[current].imgHeight;
        var scale = imgWidth / imgHeight
        var scrollLeft = (this.data.winHeight) / scale - winWidth;
        //创建动画效果
        var animation = wx.createAnimation({
            duration: 400,
            timingFunction: '"linear"',
            delay: 0,
            transformOrigin: '"50% 50% 0"',
        })
        this.animation = animation;
        animation.rotate(90).step();
        var promise = new Promise((resolve, reject) => {
            this.setData({
                Height: (this.data.winHeight) / scale,
                animationData: animation.export(),
                showModalStatus: true,
                src: this.data.path + this.data.ti[current].qa
            })
            resolve();
        })
        promise.then(() => {
            if (this.data.Height > winWidth) {
                this.setData({
                    scrollLeft: scrollLeft,
                    locate: '',
                    Width: (this.data.winHeight - 20)
                })
            } else {
                this.setData({
                    locate: 'locate',
                    Width: this.data.winHeight
                })
            }
        })
    },
    getIndex: function (e) {
        var index = e.currentTarget.dataset.index;
        this.changeTi(index - 1, this.data.ti);
        getTi(this.data.chapterId, index, (res) => {
            this.setData({
                id: res.data.ti[0].id,
                index: (--index),
                current: index,
                duration: 0
            })
        })
    },
    cancle: function () {
        this.setData({
            bgStatus: false,
        })
    },
    //点击大题选择按钮
    bindChange: function () {
        this.setData({
            bgStatus: true
        })
    },

    //滑动状态结束后改变变量
    changeTi: function (i, ti) {
        var _this = this;
        for (let j = i - 1; j >= i - 1 && j <= i + 1; j++) {
            if (j >= this.data.tiNumber || j < 0) {
                return;
            }
            if (ti[j] == undefined) {
                getTi(this.data.chapterId, j + 1, (res) => {
                    ti[j] = res.data.ti[0];
                    wx.getImageInfo({
                        src: this.data.path + res.data.ti[0].qa,
                        success: function (res) {
                            var scale = res.width / res.height;
                            var newHeight = _this.data.winWidth / scale;
                            if (newHeight <= _this.data.winHeight) {
                                ti[j].swiperHeight = _this.data.winHeight;
                            } else {
                                ti[j].swiperHeight = newHeight;
                            }
                            ti[j].newHeight = newHeight;
                            ti[j].imgWidth = res.width;
                            ti[j].imgHeight = res.height;
                            _this.setData({
                                ti: ti
                            })
                        },
                        fail: function (res) { },
                        complete: function (res) { },
                    })
                })
            }
        }
    },
    change: function (e) {
        var current = e.detail.current;
        var id = this.data.ti[current].id;
        this.changeTi(current, this.data.ti)
        this.setData({
            current: current,
            index: current,
            id: id,
        })
    },
    //点击大题选择按钮触发
    // bindPickerChange: function (e) {
    //     var arr = ['全部'];
    //     var index = e.detail.value;
    //     getTi(this.data.chapterId, ++index, (res) => {
    //         var data = res.data;
    //         var len = res.data.ti.length;
    //         if (len == 1) {
    //             this.setData({
    //                 arr: arr,
    //                 smallNum: 0,
    //                 id: data.ti[0].id
    //             })
    //         } else {
    //             for (let i = 1; i <= len; i++) {
    //                 arr.push(i)
    //             }
    //             this.setData({
    //                 arr: arr,
    //                 smallNum: 0,
    //                 id: data.id
    //             })
    //         }
    //         this.setData({
    //             ti: res.data.ti,
    //             index: e.detail.value
    //         })
    //     })
    // },
    //点击小题选择按钮触发
    // bindChangeSmall: function (e) {
    //     var index = this.data.index;
    //     getTi(this.data.chapterId, ++index, (res) => {
    //         // console.log(res)
    //         var data = res.data;
    //         if (data.ti.length == 1) {
    //             this.setData({
    //                 ti: data.ti,
    //                 id: data.ti[0].id
    //             })
    //         } else {
    //             if (e.detail.value == 0) {
    //                 this.setData({
    //                     ti: data.ti,
    //                     id: data.id,
    //                     smallNum: e.detail.value
    //                 })
    //             } else {
    //                 var ti = [];
    //                 var smallTi = data.ti[--e.detail.value];
    //                 ti.push(smallTi);
    //                 this.setData({
    //                     ti: ti,
    //                     smallNum: (++e.detail.value),
    //                     id: smallTi.id
    //                 })
    //             }
    //         }
    //     })
    // },
    //获取小题
    // getSmallTi: function (num) {
    //     var arr = ['全部'];
    //     getTi(this.data.chapterId, num, (res) => {
    //         if (res.data.ti.length == 1) {
    //             this.setData({
    //                 arr: arr,
    //             })
    //         } else {
    //             var len = res.data.ti.length;
    //             for (let i = 1; i <= len; i++) {
    //                 arr.push(i)
    //             }
    //             this.setData({
    //                 arr: arr,
    //             })
    //         }
    //     })
    // },
    getEachTi: function (i, ti) {
        // var tiNumber = this.data.tiNumber;
        var _this = this;
        if (ti.length >= 2) {
            return;
        } else {
            getTi(this.data.chapterId, ++i, (res) => {
                wx.showNavigationBarLoading();
                ti.push(res.data.ti[0]);
                wx.getImageInfo({
                    src: this.data.path + res.data.ti[0].qa,
                    success: function (res) {
                        var scale = res.width / res.height;
                        var newHeight = _this.data.winWidth / scale;
                        if (newHeight <= _this.data.winHeight) {
                            ti[i - 1].swiperHeight = _this.data.winHeight;
                        } else {
                            ti[i - 1].swiperHeight = newHeight;
                        }
                        ti[i - 1].newHeight = newHeight;
                        ti[i - 1].imgWidth = res.width;
                        ti[i - 1].imgHeight = res.height;
                        _this.setData({
                            ti: ti
                        })
                        _this.getEachTi(i, ti)
                    },
                    fail: function (res) {
                        console.log(res)
                    },
                    complete: function () {
                        if (_this.data.ti.length == 2) {
                            wx.hideNavigationBarLoading();
                        }
                    }
                })
            })
        }
        this.getTiNum();
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: options.title,
        });
        this.setData({
            chapterId: options.chapter_id,
            tiNumber: options.ti_number,
            title: options.title,
            winWidth: (wx.getSystemInfoSync().windowWidth - 5), //获取屏幕宽度
            winHeight: wx.getSystemInfoSync().windowHeight //获取屏幕高度
        });
        if (this.data.winWidth <= 320) {
            this.setData({
                padding: 0
            })
        };
        this.getEachTi(0, [])
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

        //默认加载第一大题
        getTi(this.data.chapterId, 1, (res) => {
            var data = res.data
            this.setData({
                id: data.ti[0].id
            })
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})